package com.example.demo.materialPurchase.controller;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.common.ResponseData;
import com.example.demo.entity.Department;
import com.example.demo.materialPurchase.entity.DepartMaterial;
import com.example.demo.materialPurchase.entity.MaterialBaseInfo;
import com.example.demo.materialPurchase.entity.MaterialPurchase;
import com.example.demo.materialPurchase.mapper.MaterialPurchaseMapper;
import com.example.demo.materialPurchase.service.impl.MaterialPurchaseServiceImpl;
import com.example.demo.systemManagement.mapper.DepartmentManageMapper;
import com.example.demo.systemManagement.service.CodeManageService;
import com.example.demo.systemManagement.service.DepartmentManageService;
import com.example.demo.systemManagement.service.PurchaseInfoManageService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lby
 * @since 2021-05-09
 */
@RestController
@RequestMapping("/materialPurchase/material-purchase")
@PreAuthorize("hasAnyAuthority('4','1','2')")
@CrossOrigin(origins = {"*"})
public class MaterialPurchaseController {

	@Autowired
	private MaterialPurchaseServiceImpl marterialSerImpl;
	
	@Autowired
	private MaterialPurchaseMapper matMapper;
	
    @Autowired
    private DepartmentManageMapper departManageMapper;
    
    @Autowired
    private CodeManageService codeManageService;
	
	/**
	 * 根据参数 preStatus 对 采购物料信息 状态 进行更改
	 * @param ids
	 * @param preStatus
	 * @return
	 */
	@PostMapping("/updStatus")
	public ResponseData updMaterialStatus(@RequestParam(value = "ids") String[] ids, @RequestParam("preStatus") String preStatus) {
		
		return marterialSerImpl.UpdateStatus(ids, preStatus);
		
	}
	
	/**
	 * 根据账户名称搜索所属部门名称
	 * @param account
	 * @return
	 */
	@PostMapping("/selAccount")
	public ResponseData selAccouDepart(@RequestParam("account") String account) {
		
		Department dep = new Department();
		dep = matMapper.selAccountDepart(account);
		return ResponseData.buildOk(dep);
		
	}
	/**
	 * 根据 部门名称 和 状态 查询所有 采购物料信息
	 * @param preStatus
	 * @param departName
	 * @return
	 */
	@PostMapping("/selAllMaterial")
	public ResponseData selAllMaterial(@RequestParam("preStatus") String preStatus, @RequestParam("departName") String departName,
			@RequestParam("currentPage") int currentPage , 
			@RequestParam("pageSize") int pageSize) {
			
		return marterialSerImpl.selAllPurchase(preStatus,departName,currentPage,pageSize); 
	}
	
	/**
	 * 根据id 修改 采购物料信息
	 * @param id
	 * @param purchaseNum
	 * @param partUsed
	 * @param remark
	 * @return
	 */
	@PostMapping("/updMaterial")
	public ResponseData updMaterialInformation(@RequestParam("id") String id, @RequestParam("purchaseNum") String purchaseNum, 
			@RequestParam("partUsed") String partUsed, @RequestParam("remark") String remark) {
		
		MaterialPurchase mate = new MaterialPurchase();
		mate.setId(Integer.valueOf(id));
		mate.setPartUsed(partUsed);
		mate.setPurchaseNum(Integer.valueOf(purchaseNum));
		mate.setRemark(remark);
		try {
			marterialSerImpl.updateById(mate);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ResponseData.buildError("update error！");
		}
		
		return ResponseData.buildOk("update success! ");
	}
	
	/**
	 * 根据id 删除 采购物料信息
	 * @param id
	 * @return
	 */
	@GetMapping("/delById")
	public ResponseData delMaterialById(@RequestParam("id") String id) {
		
		QueryWrapper<MaterialPurchase> wrapper = new QueryWrapper<>();
		wrapper.eq("id", id);
		
		try {
			marterialSerImpl.remove(wrapper);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ResponseData.buildError("delete error！");
		}
		
		return ResponseData.buildOk("delete success! ");
		
	}
	
	/**
	 * 根据 状态 查询 采购物料信息，并根据部门名称 对物料信息进行 分类
	 * @param PreStatus
	 * @return
	 */
	@GetMapping("/selDepMaterial")
	public ResponseData selDepartMaterial(@RequestParam("preStatus") String preStatus, @RequestParam("currentMonth") String currentMonth) {
		
		// 根据 状态 查询
		List<MaterialBaseInfo> mDatas = new ArrayList<>();
		
		// 查询所有符合条件的数据
		try {
			String current = currentMonth + "%";
			mDatas = matMapper.selAllPurByStatus(preStatus,current);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ResponseData.buildError("查询失败！");
		}
		
		// 查询所有部门信息
		List<Department> departments = new ArrayList<Department>();
		
		// 查询所有符合条件的数据
		try {
			departments = departManageMapper.selectAll();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ResponseData.buildError("查询失败！");
		}
		
		// 对 采购信息按照部门分类
		List<DepartMaterial> list = new ArrayList<DepartMaterial>();
		
		for(int i=0; i<departments.size(); i++) {
			
			// 记录每个部门的采购信息
			DepartMaterial tempDep = new DepartMaterial();
//			System.out.println("部门名称: " + departments.get(i).getDepartName());
			tempDep.setDepartmentName(departments.get(i).getDepartName());
			String departName = departments.get(i).getDepartName();
			List<MaterialBaseInfo> tempMaterial = new ArrayList<MaterialBaseInfo>();
			
			int num = 0; // 记录每个部门的数据
			for(int j=0; j<mDatas.size(); j++) {
				
				// 如果物资采购信息为所属部门，则将其加入其中
//				System.out.println("输出物资信息所属部门：" + mDatas.get(j).getDepartName());
				String mdepartName = mDatas.get(j).getDepartName();
				if(mdepartName.equals(departName)) {
//					System.out.println("输出物资信息：" + mDatas.get(j));
					tempMaterial.add(mDatas.get(j));
					num++;
				}
			}
			
			tempDep.setMaterialBase(tempMaterial);
			tempDep.setTotal(num);
			if(num > 0) { // 判断该部门下是否有 采购物资信息
				list.add(tempDep);
			}
	
		}
		
		return ResponseData.buildOk(list);
	}
	
	/**
	 * 分页 查询物料基本信息
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	@PostMapping("/selPageMateriInfo")
	public ResponseData selPageMaterials(@RequestParam("currentPage") int currentPage , 
			@RequestParam("pageSize") int pageSize) {
		
		return marterialSerImpl.selMaterialBases(currentPage, pageSize);
	}
	
	/**
	 * 查询所有物料基本信息
	 * @return
	 */
	 @GetMapping("/selectAll")
     public ResponseData selectAll() {
		 return codeManageService.selectAll();
     }
}

